﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;

public class ApplePay
{

	public delegate void PayCallback (int numid, bool result);

	public static PayCallback BuyCb;

	public static void Buy (int numid, PayCallback ac)
	{
		switch (Application.platform) {
		case RuntimePlatform.WindowsEditor:
			Debug.Log ("TJSDK" + "添加numid为" + numid + "的Buy callback");
			ac(numid,true);
			break;
		case RuntimePlatform.IPhonePlayer:
			Debug.Log ("TJSDK" + "添加numid为" + numid + "的Buy callback");
			BuyCb = ac;
			InitAndBuy (numid, PayResult);
			break;
		case RuntimePlatform.Android:
			break;
		default:
			break;
		}
	}
	
	public static void Restore (int numid, PayCallback ac)
	{
		switch (Application.platform) {
		case RuntimePlatform.WindowsEditor:
			Debug.Log ("TJSDK" + "添加numid为" + numid + "的Restore callback");
			ac(numid,true);
			break;
		case RuntimePlatform.IPhonePlayer:
			Debug.Log ("TJSDK" + "添加numid为" + numid + "的Restore callback");
			BuyCb = ac;
			RestoreBuy (numid, PayResult);
			break;
		case RuntimePlatform.Android:
			break;
		default:
			break;
		}
	}
	
	[DllImport("__Internal")]
	static extern void InitAndBuy (int numid, PayCallback payCb);
	
	[AOT.MonoPInvokeCallback(typeof(PayCallback))]
	static void PayResult (int numId, bool result)
	{
		Debug.Log ("TJSDK" + "numId 为:" + numId + " CallBack,result:"+result);
		if (BuyCb != null) {
			BuyCb (numId, result);
			switch (numId) {
			case 0:
				Debug.Log ("*****购买移除广告*****");
				break;
			case 1:
				Debug.Log ("*****购买解锁皮肤*****");
				break;
			case 2:
				Debug.Log ("*****购买金币肤*****");
				break;
			default:
				break;
			}
		}else{
			Debug.Log ("TJSDK" + "回调事件为空");
		}
	}

	[DllImport("__Internal")]
	static extern void RestoreBuy (int numid, PayCallback RestoreCb);

}
